//
//  ShangPinNeiMuViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/19.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  "商品类目"
/**需求
 1.点击底部购物车按钮可以弹出购物车
 2.请求营业时间数据
 3.点击结算按钮(如果有数据的话)可以跳转结算页面
 */

#import "ShangPinLeiMuViewController.h"
//model
#import "SPLMModel.h"
//view
#import "NewShopCartBottomView.h"
#import "ShoppingCartView.h"
#import "ShangPinLeiMuCustomView.h" //封装tableView及其代理方法

//viewController
#import "WaiSongViewController.h"
#import "WSSureOrderViewController.h" //外送确认订单
#import "FDTabBarController.h" //切换控制器


//工具类
#import "YingYeShiJianKeFuDianHuaHelper.h" //获取营业事件
#import "HomeShopCartHelper.h" //计算购物车总价

//12-30底部bottomView的高度
#define kBottomViewHeight 50

@interface ShangPinLeiMuViewController ()<
ShopCartViewDelegate,
ShoppingCartViewDelegate>


/**封装tableView及其代理方法*/
@property (nonatomic, strong) ShangPinLeiMuCustomView *customView;

/**底部购物车view*/
@property (nonatomic, strong) NewShopCartBottomView *bottomView;

/**点击购物车按钮 弹出购物车里面的内容的view 默认隐藏 点击购物车按钮显示*/
@property (nonatomic, strong) ShoppingCartView *shopcartView;

/**10-25购物车视图删除还是加载 用于标记是否显示购物车页面*/
@property (assign, nonatomic) BOOL isShopping;

/**11-21用于标记是否可以去结算
 如果计算出来的购物车金额为 < 0.0 就允许点击
 */
@property (nonatomic) NSInteger sumPrice;

@end

@implementation ShangPinLeiMuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViewAnnLoadData]; //请求本业数据
    
    
    if (self.presentingViewController !=nil) {
        FDLog(@"present进来的");
//        //12-4添加返回按钮
        [self setupBackButton];
    }
}

- (void)setupBackButton {
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [backButton setImage:[UIImage imageNamed:@"返回箭头.png"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
//    iOS10真机bug,不给frame不显示
    backButton.frame = CGRectMake(0, 0, 40, 40);
    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

- (void)backAction {
    FDLog(@"弹尽粮绝减肥的会计法律的");
    [self dismissViewControllerAnimated:YES completion:nil];
    
    //切换控制器
    UIApplication *app = [UIApplication sharedApplication];
    //确保只有一个shareFDTabarVC
    FDTabBarController *tabVC = [FDTabBarController shareFDTabarVC];
    tabVC.selectedIndex = 0; //首页
    app.keyWindow.rootViewController = tabVC;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [HomeShopCartHelper shopcartHelperTotalPriceBlock:^(CGFloat totalPrice) {
        
        [self.bottomView updateshopcartBottomViewTotalPrice:totalPrice];
        
        self.sumPrice = totalPrice; //标记购物车金额是否为0
    }];
    RBFunctionLog;
}

- (void)setupViewAnnLoadData {
    
    self.view.backgroundColor = [UIColor whiteColor];
    //国际化
    NSString *splmStr = NSLocalizedString(GoodsList, @"商品类目");
    self.navigationItem.title = splmStr;
    
    //TABLEView
    //tableViweframe ? 为什么cell和tv的顶部和底部都有间距？
    CGRect tempFram = self.view.bounds;
//    tempFram.origin.y = FD_NavH; //yo
//    tempFram.size.height = SCREEN_HEIGHT - FD_NavH - FD_TabbarSafeBottomMargin - 50;
    tempFram.size.height = SCREEN_HEIGHT - FD_TabbarSafeBottomMargin - kBottomViewHeight;

    self.customView.frame = tempFram;
//    self.tv.contentInset = UIEdgeInsetsMake(-10, 0, -10, 0);
    [self.view addSubview:self.customView];
    
    //底部购物车view
    CGFloat bottomView_y = self.customView.rb_bottom;
    self.bottomView.frame = CGRectMake(0, bottomView_y, SCREEN_WIDTH, kBottomViewHeight);
    [self.view addSubview:self.bottomView];
    
//    [self requestShangPinLeiMiData]; //请求商品类目数据
    
    [YingYeShiJianKeFuDianHuaHelper obtainYingYeShiJianComplectionBlock:^(NSString * _Nonnull active, NSString * _Nonnull quiet) {
        [self.bottomView updateYingYeShiJianBegin:active end:quiet];
    }]; //请求业营业时间
    
    [self.customView requestShangPinLeiMiData]; //请求本页数据
    
    
    //2019-2-23只有点击首页购物按钮才自动展开购物车列表反之不弹框
    if (self.isShangPinLeiMuViewControllerShowCartList) {
        [self newDemand2019215NoDataNotExpandCartList]; //2019-2-16
    }
}


#pragma mark - 四 底部购物车事件 ShopCartViewDelegate
- (void)shopcartViewDidClickCartBtn:(UIButton *)btn {
    RBFunctionLog;
    
    [self newDemand2019215NoDataNotExpandCartList];
}

//!!!!:2019-2-16新加需求
/**
 1.一进来:如果有数据购物车自动展开
 2.如果没有数据点击“购物车按钮” 不展开购车,同时提示@"购物车暂无商品" ✅
 /user/cart/list
 */
- (void)newDemand2019215NoDataNotExpandCartList {
    [HomeShopCartHelper shopcartHelperCarListIsEmptyBlock:^(BOOL isHaveData) {
        FDLog(@"111:%@",isHaveData ? @"有数据" :@"没有数据不展开");
        
        if (isHaveData) { //有数据才暂开购物车列表
            [self expandCartListView];
        }else { //没有数据不暂开
            [JRToast showWithText:NSLocalizedString(Home_ShopCartNoGoods, @"购物车暂无商品")];
        }
    }];
}

- (void)expandCartListView {
    if (!_isShopping) {
        self.shopcartView=[[ShoppingCartView alloc]init];
        self.shopcartView.frame =CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH,SCREEN_HEIGHT-FD_NavH-FD_TabbarSafeBottomMargin);
        self.shopcartView.delegate = self; //设置代理
        
        //蒙版：蒙版为什么不放在里面呢？
        UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(0, FD_NavH, SCREEN_WIDTH,SCREEN_HEIGHT-kBottomViewHeight-FD_NavH-FD_TabbarSafeBottomMargin)];
        bgView.tag = 111;
        bgView.backgroundColor = [UIColor rb_colorHex:0x000000 alpha:0.5];
        [self.view addSubview:bgView];
        
        //显示购物车
        [self.shopcartView addShoppingCartView:self];
        
        [UIView animateWithDuration:.3 animations:^{
            //适配iphoneX
            self.shopcartView.frame =CGRectMake(0, FD_NavH, SCREEN_WIDTH,SCREEN_HEIGHT-kBottomViewHeight-FD_NavH-FD_TabbarSafeBottomMargin);
        } completion:^(BOOL finished) {
        }];
    } else{
        [self.shopcartView removeSubView:self];
    }
    _isShopping = !_isShopping;
}




- (void)shopcartViewDidClickJieSuanBtnAction:(UIButton *)btn {
    RBFunctionLog;
    
    if (self.sumPrice < 0.1) {
        [JRToast showWithText:NSLocalizedString(Home_ShopCartNoGoods, @"购物车暂无商品")];
        return;
    }
    
    //2019-2-21隐藏购物车列表
    [self.shopcartView removeSubView:self];

    //跳转到确认订单页面
    WSSureOrderViewController *sureOrderVC = [[WSSureOrderViewController alloc] init];
    [self.navigationController pushViewController:sureOrderVC animated:YES];
}

#pragma mark - 四-2 ShoppingCartViewDelegate
/**用于更新底部总金额，添加移除购物车之后要，从新计算总价。并通过代理的方式通知控制器
 让控制器告知 ShopCartBottomView 更新总金额*/
- (void)shoppingCartViewUpdateTotalPrice:(CGFloat)totalPrice {
    RBFunctionLog
    [self.bottomView updateshopcartBottomViewTotalPrice:totalPrice];
}

/**购物车被清空通知控制器移除购物车页面*/
- (void)shoppingCartHaveBeClean {
    RBFunctionLog
    [self.shopcartView removeSubView:self]; //从当前控制器消失
    
    self.sumPrice = 0; //标记购物车金额是否为0
}

#pragma mark -    getter
- (ShangPinLeiMuCustomView *)customView {
    if (!_customView) {
        _customView = [[ShangPinLeiMuCustomView alloc] init];
        
        FDWeakSelf;
        //事件回调
        _customView.splmCustomViewClickCellblock = ^(SPLMModel * _Nonnull splmModel) {
            WaiSongViewController *wsVC = [WaiSongViewController new];
            wsVC.shangPinLeiMuId = splmModel.splmId;
            
            //跳转
            [weakSelf.navigationController pushViewController:wsVC animated:YES];
        };
    }
    return _customView;
}

- (NewShopCartBottomView *)bottomView {
    if (!_bottomView) {
        //view的高度
        CGFloat sh = self.view.bounds.size.height;

        CGFloat y = sh - 50-FD_NavH-FD_TabbarSafeBottomMargin;
        //设置的代理
        _bottomView = [[NewShopCartBottomView alloc] initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, kBottomViewHeight)];
        _bottomView.delegate = self; //设置代理 ShopCartViewDelegate
    }
    return _bottomView;
}


- (void)dealloc {
    FDLog(@"销毁了销毁了xxxx");
}
@end
